﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TrafficArchives.Common;
using TrafficArchives.DataAccess;
using TrafficArchives.Logs;
using TrafficArchives.Models;

namespace TrafficArchives.SafeEducate
{
    public partial class SafeEducateProcess : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BtnDel.Attributes.Add("onclick", "if (confirmCanel()){return true;}else{return false;}");
                QueryData();
            }
        }

        private void QueryData()
        {
            var request = new Request { Pagination = { Enabled = true, PageSize = MainPager.PageSize, PageIndex = MainPager.CurrentPageIndex - 1 } };

            var db = new SafeEducateDb();
            var data = db.GetData(request);
            MainPager.RecordCount = data.RecordCount;
            GridViewMain.DataSource = data.Data;
            GridViewMain.DataBind();

            // 没数据时，显示表头。
            TableHeader.Visible = data.RecordCount == 0;
            UpdatePanelMain.Update();
        }

        /// <summary>
        /// 数据处按钮处理。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridViewMainRowCommand(object sender, GridViewCommandEventArgs e)
        {
            var id = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "Modify":
                    Response.Redirect(string.Format("SafeEducateAdd.aspx?action=edit&id={0}", id));
                    break;
                case "Look":
                    Response.Redirect(string.Format("SafeEducateAdd.aspx?action=look&id={0}", id));
                    break;
            }
        }

        protected void MainPagerPageIndexChanged(object sender, EventArgs e)
        {
            QueryData();
        }

        protected void BtnQueryClicked(object sender, EventArgs e)
        {
            MainPager.CurrentPageIndex = 1;

            QueryData();
        }

        protected void BtnDelClicked(object sender, EventArgs e)
        {
            var ids = new List<string>();
            foreach (GridViewRow row in GridViewMain.Rows)
            {
                var checkbox = (CheckBox)row.FindControl("ChkRow");
                if (checkbox.Checked)
                {
                    var dataKey = GridViewMain.DataKeys[row.RowIndex];
                    if (dataKey != null)
                    {
                        var id = dataKey.Value.ToString();
                        ids.Add(id);
                    }
                }
            }
            if (ids.Count > 0)
            {
                var db = new SafeEducateDb();
                db.Delete(ids);
                //var log = new LogInfo(LogType.);
                //LogWriter.Writer(log);
                BtnQueryClicked(null, null);
            }
        }
    }
}